<template>
	<el-container>
		<el-header>
			<div class="right-panel">
				<div class="right-panel-search">
					<table>
						<tr>
							<td>所属模块</td>
							<td>
								<el-select v-model="search.modules">
									<el-option
										v-for="item in options"
										:key="item.value"
										:label="item.label"
										:value="item.value">
									</el-option>
								</el-select>
							</td>
							<td>扩展名</td>
							<td>
								<el-input v-model="search.extName" placeholder="扩展名"></el-input>
							</td>
							<td>开始时间</td>
							<td>
								<el-date-picker v-model="search.beginTime" placeholder="开始时间" type="date"
												value-format="YYYY-MM-DD"></el-date-picker>
							</td>
							<td>-</td>
							<td>
								<el-date-picker v-model="search.endTime" placeholder="结束时间" type="date"
												value-format="YYYY-MM-DD"></el-date-picker>
							</td>
							<td>文件名</td>
							<td>
								<el-input v-model="search.keyword" placeholder="文件名模糊查询"></el-input>
							</td>
							<td>上传人</td>
							<td style="display: flex;">
								<select-user-dialog v-model:accountId="search.createAccount" :mode="false"/>
							</td>
						</tr>
					</table>
				</div>
				<div>
					<el-button type="primary" icon="el-icon-search" @click="upsearch" style="float: right;"></el-button>
				</div>
			</div>
		</el-header>
		<el-main class="nopadding">
			<scTable ref="table" :apiObj="apiObj" row-key="attachId" stripe>
				<el-table-column label="#" type="index" width="50"></el-table-column>
				<el-table-column label="文件名称" prop="newName" width="400" :show-overflow-tooltip="true">
					<template #default="scope">
						{{ scope.row.newName.length > 30 ? scope.row.newName.substr(0, 30) + '...' + scope.row.extName : scope.row.newName }}
					</template>
				</el-table-column>
				<el-table-column label="当前文件名称" prop="fileName" width="400" :show-overflow-tooltip="true">
					<template #default="scope">
						{{ scope.row.fileName.length > 30 ? scope.row.fileName.substr(0, 30) + '...' + scope.row.extName : scope.row.fileName }}
					</template>
				</el-table-column>
				<el-table-column label="扩展名" prop="extName" width="80"></el-table-column>
				<el-table-column label="所属模块" prop="modules" width="100">
					<template #default="scope">
						{{ getModuleName(scope.row.modules) }}
					</template>
				</el-table-column>
				<el-table-column label="当前状态" prop="delFlag" width="80">
					<template #default="scope">
						<el-tag type="success" v-if="scope.row.delFlag=='0'">正常</el-tag>
						<el-tag type="danger" v-else-if="scope.row.delFlag=='1'">已删</el-tag>
					</template>
				</el-table-column>
				<el-table-column label="上传人" prop="createUserName" width="100"></el-table-column>
				<el-table-column label="创建时间" prop="createTime" width="180"></el-table-column>
				<el-table-column label="文件版本" prop="version" min-width="80"></el-table-column>
				<el-table-column label="文件大小" prop="fileSize" min-width="80">
					<template #default="scope">
						{{ parseFloat(scope.row.fileSize / (1024 * 1024)).toFixed(2) }}M
					</template>
				</el-table-column>
				<el-table-column label="相关附件" prop="attachId" min-width="300" :show-overflow-tooltip="true">
					<template #default="scope">
						<attach-menu :attachIds="scope.row.attachId" :roleFlag="3" :preview="true"></attach-menu>
					</template>
				</el-table-column>
				<el-table-column label="百度网盘" prop="baiduPanDownLink" min-width="80">
					<template #default="scope">
						<el-button text type="primary" size="small" @click="table_down(scope.row, scope.$index)"
								   v-if="scope.row.baiduPanDownLink!=undefined">下载
						</el-button>
					</template>
				</el-table-column>
				<el-table-column label="操作" fixed="right" align="right" width="170">
					<template #default="scope">
						<el-button-group>
							<el-button text type="primary" size="small" @click="readFile(scope.row, scope.$index)">
								预览
							</el-button>
							<el-popconfirm title="确定删除吗？" @confirm="table_del(scope.row, scope.$index)">
								<template #reference>
									<el-button text type="primary" size="small">删除</el-button>
								</template>
							</el-popconfirm>
						</el-button-group>
					</template>
				</el-table-column>

			</scTable>
		</el-main>
	</el-container>
</template>

<script>
import selectUserDialog from "@/views/module/selectUserDialog";
import sysConfig from "@/config";

export default {
	name: 'Attach Manage',
	components: {
		selectUserDialog,
	},
	data() {
		return {
			apiObj: this.$API.file.attach.getAttachManageList,
			options: [{
				value: '',
				label: '全部'
			}, {
				value: 'notice',
				label: '通知公告'
			}, {
				value: 'meeting',
				label: '会议附件'
			}, {
				value: 'meeting',
				label: '会议附件'
			}, {
				value: 'bpm',
				label: '流程附件'
			}, {
				value: 'doc',
				label: '收发文附件'
			}, {
				value: 'email',
				label: '邮件附件'
			}, {
				value: 'publicfile',
				label: '公共附件'
			}, {
				value: 'personalfile',
				label: '个人附件'
			}, {
				value: 'diary',
				label: '日志附件'
			}, {
				value: 'leadermailbox',
				label: '领导信箱附件'
			}, {
				value: 'hr',
				label: '人力资源'
			}, {
				value: 'task',
				label: '任务附件'
			}, {
				value: 'datainfo',
				label: '信息上报附件'
			}, {
				value: 'project',
				label: '项目附件'
			}, {
				value: 'budget',
				label: '预算附件'
			}, {
				value: 'knowledge',
				label: '知识附件'
			}, {
				value: 'template',
				label: '模版附件'
			}, {
				value: 'archives',
				label: '档案附件'
			}, {
				value: 'filemap',
				label: '文档地图'
			}, {
				value: 'other',
				label: '其它'
			}],
			search: {
				keyword: '',
				createAccount: '',
				extName: '',
				beginTime: '',
				endTime: ''
			}
		}
	},
	mounted() {
	},
	methods: {
		//删除
		async table_del(row) {
			var reqData = {attachId: row.attachId}
			var res = await this.$API.file.attach.deleteAttach.post(reqData);
			if (res.code == 200) {
				this.$refs.table.refresh()
				this.$message.success("删除成功")
			} else {
				this.$alert(res.message, "提示", {type: 'error'})
			}
		},
		async table_show(row) {
			console.log(row);
		},
		async table_down(row) {
			window.open(row.baiduPanDownLink+"&ddtab=true");
		},
		//搜索
		async upsearch() {
			this.$refs.table.upData(this.search)
		},
		getModuleName(module) {
			var labelStr = "";
			this.options.forEach(item => {
				if (item.value == module) {
					labelStr = item.label;
				}
			})
			return labelStr;
		},
		readFile(row, index) {
			let attachId = row.attachId;
			let extName = row.extName;
			if (this.$TOOL.data.get("OPEN_ON_LINE_FLAG") === '0') {
				this.$nextTick(() => {
					window.open('/#/file/preview?attachId=' + attachId + "&extName=" + extName, "_blank");
				})
			} else if (this.$TOOL.data.get("OPEN_ON_LINE_FLAG") === '2') {
				if (sysConfig.DOC_FILE.includes(extName)) {
					if (extName === '.docx') {
						this.$nextTick(() => {
							window.open('/#/file/preview/word?attachId=' + attachId + "&extName=" + extName, "_blank");
						})
					} else {
						this.$nextTick(() => {
							window.open('/#/file/preview?attachId=' + attachId + "&extName=" + extName, "_blank");
						})
					}
				} else if (sysConfig.EXCEL_FILE.includes(extName)) {
					if (extName === '.xlsx') {
						this.$nextTick(() => {
							window.open('/#/file/preview/excel?attachId=' + attachId + "&extName=" + extName, "_blank");
						})
					} else {
						this.$nextTick(() => {
							window.open('/#/file/preview?attachId=' + attachId + "&extName=" + extName, "_blank");
						})
					}
				} else if (sysConfig.PPT_FILE.includes(extName)) {
					this.$nextTick(() => {
						window.open('/#/file/preview?attachId=' + attachId + "&extName=" + extName, "_blank");
					})
				} else if (sysConfig.PDF_FILE.includes(extName)) {
					this.openPdfFile(attachId, extName, 1);
				} else if (sysConfig.IMG_FILE.includes(extName)) {
					const image = new Image()
					image.src = sysConfig.API_URL + '/get/file/getFileDown?attachId=' + attachId + "&extName=" + extName;
					image.onload = () => {
						image.style.margin = "0 auto"
						image.style.display = "block"
						const newWin = window.open("", "_blank")
						newWin.document.write(image.outerHTML)
					}
				}
			}
			this.$emit('success', this.uid);
		},
		openPdfFile(attachId, extName, openModeType) {
			if (this.$TOOL.data.get("OFD_TYPE") === '0') {
				if(extName=='.ofd')
				{
					this.$nextTick(() => {
						window.open('/#/file/preview/ofd?attachId=' + attachId + "&extName=" + extName, "_blank");
					})
				}else {
					this.$nextTick(() => {
						window.open('/#/file/preview/pdf?attachId=' + attachId + "&extName=" + extName, "_blank");
					})
				}
			} else if (this.$TOOL.data.get("OFD_TYPE") === '1') {
				this.$nextTick(() => {
					window.open(this.$TOOL.data.get("ATTACH_HOST") + '/office/ofd/openfxofd?attachId=' + attachId + "&openModeType=" + openModeType + "&extName=" + extName, "_blank");
				})
			} else if (this.$TOOL.data.get("OFD_TYPE") === '2') {
				this.$nextTick(() => {
					window.open(this.$TOOL.data.get("ATTACH_HOST") + '/office/ofd/openswofd?attachId=' + attachId + "&openModeType=" + openModeType + "&extName=" + extName, "_blank");
				})
			}
		},
	}
}
</script>

<style>
</style>
